#ifndef _SCUDB_SYNC_BLIST_H_
#define _SCUDB_SYNC_BLIST_H_

#include "sd_global.h"
#include "sd_thread.h"

struct sd_SyncBlist;
struct sd_MemoryAllocator;

typedef struct sd_SyncBlistNode
{
	struct sd_SyncBlist* blist;
	struct sd_SyncBlistNode* next;
	size_t usedcount;
	size_t capacity;
	sd_RWLock lock;
	uint8  buf[1];
}sd_SyncBlistNode;

typedef struct sd_SyncBlist
{
	size_t elemsize;
	size_t node_cap;
	sd_SyncBlistNode* firstNode;
	struct sd_MemoryAllocator* ma;
}sd_SyncBlist;

typedef struct sd_SyncBlistIterator
{
	sd_SyncBlist* blist;
	sd_SyncBlistNode* curnode;
	int curindexInNode;
	bool bWLock;
	bool bCurrentNodeLocked;
	bool bLockList;
}sd_SyncBlistIterator;

sd_Ret sd_SyncBlistCreate(sd_SyncBlist* blist, size_t elemsize, size_t node_cap, struct sd_MemoryAllocator* ma);
sd_Ret sd_SyncBlistFree(sd_SyncBlist* blist);
sd_Ret sd_SyncBlistInsert(sd_SyncBlist* blist, const void* elem);
sd_Ret sd_SyncBlistInsertTogether(sd_SyncBlist* blist, const void* elem1, size_t elem1size, const void* elem2);
sd_Ret sd_SyncBlistInsertTogetherWithoutLock(sd_SyncBlist* blist, const void* elem1, size_t elem1size, const void* elem2);
sd_Ret sd_SyncBlistEmpty(sd_SyncBlist* blist);
sd_Ret sd_SyncBlistBegin(sd_SyncBlist* blist, sd_SyncBlistIterator* iter, bool bWLock, bool bLockList);
sd_Ret sd_SyncBlistNext(sd_SyncBlistIterator* iter);
sd_Ret sd_SyncBlistGet(sd_SyncBlistIterator* iter, void* elem);
sd_Ret sd_SyncBlistPartialGet(sd_SyncBlistIterator* iter, void* key, int offset, size_t size);
sd_Ret sd_SyncBlistReleaseLock(sd_SyncBlistIterator* iter);
sd_Ret sd_SyncBlistRemoveAndReleaseLock(sd_SyncBlistIterator* iter);
sd_Ret sd_SyncBlistClear(sd_SyncBlist* blist);
sd_Ret sd_SyncBlistUpdate(sd_SyncBlistIterator* iter, const void* elem);
sd_Ret sd_SyncBlistUpdateTogether(sd_SyncBlistIterator* iter, const void* elem1, size_t elem1size, const void* elem2);


#endif
